//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	CamlQueryBuilderOrderItem.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

namespace Smart.Common.Entities
{
	/// <summary>
	/// QueryBuilder Query Order Item
	/// </summary>
	public class CamlQueryBuilderOrderItem<TExecutorType> : CamlQueryBuilderItem<TExecutorType>
		where TExecutorType : class
	{
		/// <summary>
		/// Field
		/// </summary>
		public string Field { get; set; }

		/// <summary>
		/// QueryBuilder Query Order Item
		/// </summary>
		/// <param name="field">Field name</param>
		/// <param name="op">Operator</param>
		public CamlQueryBuilderOrderItem(string field, string op)
			: base(op)
		{
			this.Field = field;
		}

		/// <summary>
		/// Return Cache code
		/// </summary>
		public override int GetCacheCode()
		{
			return
				(Field == null ? 0 : Field.GetHashCode()) ^
				(Op == null ? 0 : Op.GetHashCode());
		}

		/// <summary>
		/// Format QueryBuilder statement
		/// </summary>
		public override string FormatQuery()
		{
			return
				string.Format("<FieldRef Name=\"{0}\" Ascending=\"{1}\" />", (Query == null ? Field : Query.Meta.FieldName(Field)), (Op.EqIC("DESC") ? "FALSE" : "TRUE"));
		}
	}
}